﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Obra.PowerShell.SmugMug
{
    /// <summary>
    /// Class to manage path manipulation for a PS cmdlet.
    /// </summary>
    /// <remarks>
    /// <para>
    /// Nothing about this class is specific to SmugMug, and this class could
    /// easily be moved out to a generic assembly for use in other providers.
    /// </para>
    /// </remarks>
    internal class Path
    {
        public const string PATH_SEPARATOR = "\\";

        private string root;

        /// <summary>
        /// c-tor
        /// </summary>
        /// <param name="root">
        /// Root of this path class, e.g. smugmug:
        /// </param>
        public Path(
            string root)
        {
            this.root = root;
        }

        #region Static Helper Methods

        /// <summary>
        /// Combine the specified root with the path components.
        /// </summary>
        internal static string Combine(
            string root,
            params string[] components)
        {
            string child = String.Join(PATH_SEPARATOR, components);
            return System.IO.Path.Combine(root, child);
        }

        /// <summary>
        /// Get the name of the leaf element in the specified path        
        /// </summary>
        /// <param name="path">
        /// The full or partial provider specific path
        /// </param>
        /// <returns>
        /// The leaf element in the path
        /// </returns>
        internal static string GetChildName(string path)
        {
            string normalizedPath = Path.Normalize(path).TrimEnd(
                Path.PATH_SEPARATOR.ToCharArray());

            int index = normalizedPath.LastIndexOf(Path.PATH_SEPARATOR);

            return (index == -1) ? 
                path :
                normalizedPath.Substring(index + 1);
        }

        /// <summary>
        /// Determines whether [is valid path] [the specified path].
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>
        /// 	<c>true</c> if [is valid path] [the specified path]; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsValidPath(
            string path)
        {
            if (String.IsNullOrEmpty(path))
            {
                return false;
            }

            return Path.Normalize(path).Split(PATH_SEPARATOR.ToCharArray()).All(
                x => !String.IsNullOrEmpty(x));
        }

        /// <summary>
        /// Support Windows and UNIX style path separators.
        /// </summary>
        internal static string Normalize(
            string path)
        {
            if (!String.IsNullOrEmpty(path))
            {
                return path.Replace("/", PATH_SEPARATOR);
            }

            return path;
        }

        #endregion
    }
}